<?php

class AutocompleteController extends Controller
{
	public function actionByCcaName()
	{
		if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
		{
			$key = $_GET['q'];
			$criteria = new CDbCriteria;
			$criteria->condition = "name LIKE '%".$key."%'";
			$result = Cca::model()->findAll($criteria);
			$returnVal = '';
			foreach($result as $resultItem)
			{
				$returnVal .= $resultItem->getAttribute('name')."\n";	
			}
			echo $returnVal;
		}
	}
	
	public function actionByRoleName()
	{
		if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
		{
			$key = $_GET['q'];
			$criteria = new CDbCriteria;
			$criteria->condition = "name LIKE '%".$key."%'";
			$result = Role::model()->findAll($criteria);
			$returnVal = '';
			foreach($result as $resultItem)
			{
				$returnVal .= $resultItem->getAttribute('name')."\n";	
			}
			echo $returnVal;
		}
	}

	public function actionByResidentName()
	{
		if(Yii::app()->request->isAjaxRequest && isset($_GET['q']))
		{
			$key = $_GET['q'];
			$criteria = new CDbCriteria;
			$criteria->condition = "name LIKE '%".$key."%'";
			$result = Resident::model()->findAll($criteria);
			$returnVal = '';
			foreach($result as $resultItem)
			{
				$returnVal .= $resultItem->getAttribute('name').' ('.$resultItem->getAttribute('matric_no').")\n";	
			}
			echo $returnVal;
		}
	}

	public function actionIndex()
	{
		$this->render('index');
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}